package pl.inea;

import pl.inea.config.KeyPreferences;
import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class LoginDialog extends Activity implements OnClickListener {
    private final String USER = "kacper";

    private SharedPreferences sharedPreferences;

    private Button buttonLogin, buttonExit;
    private EditText editTextUsername;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	setContentView(R.layout.login_dialog);

	editTextUsername = (EditText) findViewById(R.id.username);

	buttonLogin = (Button) findViewById(R.id.buttonLogin);
	buttonLogin.setOnClickListener(this);

	buttonExit = (Button) findViewById(R.id.buttonExit);
	buttonExit.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
	int id = v.getId();

	switch (id) {
	case R.id.buttonLogin:
	    if (editTextUsername.getText().toString().equals(USER)) {
		sharedPreferences = PreferenceManager
			.getDefaultSharedPreferences(LoginDialog.this);
		sharedPreferences.edit()
			.putBoolean(KeyPreferences.IS_USER_LOGGED_IN, true)
			.commit();
		setResult(RESULT_OK);
		finish();
	    } else
		Toast.makeText(LoginDialog.this,
			"Podałeś niepoprawne dane logowania!",
			Toast.LENGTH_SHORT).show();
	    break;
	case R.id.buttonExit:
	    setResult(RESULT_CANCELED);
	    finish();
	    break;
	}
    }
}
